<?php

class BaseController extends Controller{

    public $member;
    public $setting;

    public function init(){
        parent::init();
        $this->getUser();
        $this->checkLang();
        $this->getSetting();
    }

    public function getUser(){
        $MemberModel = new MemberModel();
        $member_id = $MemberModel->checkLogin();
        if($member_id){
            $this->member = $MemberModel->getUserInfo($member_id);
            if($this->member) $this->assign('member',$this->member);
            if(!$this->member['auth'] && (CONTROLLER_NAME!='Ucenter' && ACTION_NAME!="index") ){
                redirect(url('ucenter/index'));
            }
        }
        
    }

    //检测当前语言
    public function checkLang(){
        $lang = Cookie::getInstance()->get('lang');
        $langList = Yaf\Registry::get("config")->lang->toArray();
        $langArr  = explode(',', $langList['list']);
        if(!in_array($lang,$langArr)){
            $lang = $langList['default'];
        }
        $this->assign('lang',$lang);
    }

    //设置语言
    public function set_langAction(){
        $lang = $this->input('get','lang');
        $langList = Yaf\Registry::get("config")->lang->toArray();
        $langArr  = explode(',', $langList['list']);
        if(!in_array($lang,$langArr)){
            $lang = $langList['default'];
        }
        $res = Cookie::getInstance()->set('lang',$lang);
        if($res){
            $this->ajaxReturn(array('err'=>0,'lang'=>$lang));
        }
    }

    public function getSetting(){
        $SettingModel = new SettingModel();
        $this->setting = $SettingModel->getField('k,v',true);
        $this->assign('setting',$this->setting);
    }

    public function auth($mark,$data){
        //判断用户验证等级
        if($this->member['auth_level']!=$data['auth_level']){
            return 1;
        }
        if($this->member['auth_level']==0){
            return 2;
        }
        if($this->member['auth_level']==1 || $this->member['auth_level']==3){
            $Rsa = new Rsa();
            $code = Cookie::getInstance()->get(md5($mark));
            $decode = $Rsa->privDecrypt($code);
            $decode = json_decode($decode,true);
            if(time()-$decode['time']>180){
                return 3;
            }
            if($decode['code']!==$data['email_code']){
                return 4;
            }
        }
        if($this->member['auth_level']==2 || $this->member['auth_level']==3){
            Yaf\loader::import(APP_PATH.'/common/lib/GoogleAuthenticator.php');//公共方法
            $ga = new PHPGangsta_GoogleAuthenticator();
            $result = $ga->verifyCode($this->member['auth'], $data['google_code'], 2);
            if(!$result) return 5;
        }
        return 0;
    }

    public function send_email($email,$mark,$title='code',$tpl='login_email_html'){
        $MemberModel = new MemberModel();
        $cache = Cache::getInstance('Redis');
        if(!Validate::isEmail($email)){
            return array('err'=>1,'msg'=>L('_email_err'));
        }
        if( $cache->get('post:'.$email) ){
            return array('err'=>3,'msg'=>L('_one_minute_later'));
        }
        $cache->set('post:'.$email,1,60);
        $Rsa = new Rsa();
        $data['code'] = randCode(8,0);
        $data['key'] = $mark;
        $data['time'] = time();
        $code = $Rsa->pubEncrypt(json_encode($data));
        $html=L($tpl);
        $html = str_replace(array('{1}','{2}','{3}'),array($this->setting['web_title'],$email,$data['code']), $html);
        $res = sendMail($email,$title,$html,$this->setting);
        if($res){
            Cookie::getInstance()->set(md5($mark),$code);
            return array('err'=>0,'msg'=>L('_send_success'));
        }else{
            return array('err'=>2,'msg'=>L('_send_err'));
        }
    }

}